﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Dogshow.Win.DataAccess;
using Dogshow.Win.Models;
using Ninject;
using Shell;
using System.Linq;
using Dogshow.Win.Helpers;
using Shell.Events;

namespace Dogshow.Win.Views
{
    public partial class SifarnikMestaView : ViewBase
    {
        private BindingList<Mesto> m_Mesta = new BindingList<Mesto>();

        private readonly IDataService m_DataService = ((IKernel) Bootstrap.Bootstrapper.Container).Get<IDataService>();

        public SifarnikMestaView()
        {
            InitializeComponent();
            mestoBindingSource.CurrentChanged += (s, e) =>
            {
                dataLayoutControl1.Enabled = Current != null;
            };
            dataLayoutControl1.Enabled = false;
        }

        protected override void OnLoad(EventArgs e)
        {
            OnRefresh();
        }

        public override void OnRefresh()
        {
            var mesta = m_DataService.VratiMesta1().Select(ModelMapper.Map).ToList();
            m_Mesta = new BindingList<Mesto>(mesta);
            mestoBindingSource.DataSource = m_Mesta;
        }

        public override void OnSave()
        {
            if (Current != null && !Current.IsValid)
            {
                OnAlertChanged(new WarningAlertEventArgs("Podaci nisu validni: " + Current.GetErrors()));
            }
            m_DataService.Sacuvaj(ModelMapper.Map(Current));
            OnRefresh();
        }

        public override void OnNew()
        {
            var novo = m_Mesta.AddNew();
            mestoBindingSource.MoveTo(novo);
        }

        private Mesto Current
        {
            get { return mestoBindingSource.Current as Mesto; }
        }
    }
}